package com.superdry.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSON;
import com.drysurper.R;
import com.superdry.json.Address;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.SPHelper;
import com.superdry.utils.StringUtils;
import com.superdry.utils.ToastHelper;

public class Add_AddressActivity extends Activity implements OnClickListener {

	private RelativeLayout add_address_goback;
	private TextView finished;
	private EditText name;
	private EditText phone;
	private EditText address;
	private AbHttpUtil mAbHttpUtil = null;
	private String cmd = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_add_address);
		initViews();
	}

	private void initViews() {

		cmd = getIntent().getStringExtra("cmd");
		// 获取Http工具类
		mAbHttpUtil = AbHttpUtil.getInstance(this);
		mAbHttpUtil.setTimeout(10000);

		add_address_goback = (RelativeLayout) findViewById(R.id.add_address_goback);
		finished = (TextView) findViewById(R.id.finished);

		name = (EditText) findViewById(R.id.name_et);
		phone = (EditText) findViewById(R.id.tel_et);
		address = (EditText) findViewById(R.id.address_et);

		add_address_goback.setOnClickListener(this);
		finished.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.add_address_goback:
			finish();
			break;
		case R.id.finished:
			toPlaceOrder();
			break;

		default:
			break;
		}
	}

	private void toPlaceOrder() {

		final String str_name = name.getText().toString();
		final String str_phone = phone.getText().toString();
		final String str_address = address.getText().toString();

		if (StringUtils.isEmpty(str_name)) {
			ToastHelper.show(this, "请填写姓名");
			return;
		}
		if (StringUtils.isEmpty(str_phone)) {
			ToastHelper.show(this, "请填写手机号");
			return;
		}
		if (StringUtils.isEmpty(str_address)) {
			ToastHelper.show(this, "请填写地址");
			return;
		}
		AbRequestParams params = new AbRequestParams();
		params.put("peopleName", str_name);
		params.put("phoneNumber", str_phone);
		params.put("addressName", str_address);
		params.put("userID", SPHelper.getUserInfo(this).getUserID() + "");
		mAbHttpUtil.post(GlobalConst.GET_ADD_ADDRESS, params, new AbStringHttpResponseListener() {

			@Override
			public void onStart() {

			}

			@Override
			public void onFinish() {

			}

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
				ToastHelper.show(Add_AddressActivity.this, " 地址添加失败，请稍后重试");
			}

			@Override
			public void onSuccess(int statusCode, String content) {
				List<Address> addresses = JSON.parseArray(content, Address.class);
				if (StringUtils.isEmpty(cmd)) {
					Intent intent = new Intent(Add_AddressActivity.this, PlaceOrderActivity.class);
					intent.putExtra("address", addresses.get(0));
					startActivity(intent);
				} else {
					Intent data = new Intent();
					data.putExtra("address", addresses.get(addresses.size() - 1));
					setResult(0, data);
					finish();
				}

			}
		});

	}
}
